package com.hhz.generation.util;

/**
 * @author Hz~
 * @version 1.0
 * @date 2020/11/25 14:32
 * @Description
 */

import com.hhz.generation.domain.TableData;
import com.hhz.generation.constant.Constant;
import com.hhz.generation.domain.BaseData;
import com.tele.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;
import java.util.ArrayList;

/**
 * @author: Mr.Feng
 * @create: 2018-08-06
 **/
public class DatabaseUtil {
    private final static Logger LOGGER = LoggerFactory.getLogger(DatabaseUtil.class);

    static {
        try {
            Class.forName(Constant.DRIVER);
        } catch (ClassNotFoundException e) {
            LOGGER.error("can not load jdbc driver", e);
        }
    }

    /**
     * 根据表名称获取元数据
     *
     * @param tableName 表名称
     * @return {@link BaseData}
     */
    public static BaseData getBaseDataList(String tableName) {
        Connection conn = getConnection();
        PreparedStatement pStemt;
        String tableSql = Constant.SQL + tableName;
        BaseData baseData = new BaseData();
        ArrayList<TableData> tableDatas = new ArrayList<>();
        ResultSet rs = null;
        try {
            pStemt = conn.prepareStatement(tableSql);
            rs = pStemt.executeQuery("select column_name name, data_type type, column_comment comment from information_schema.columns where  table_name = '" + tableName + "'");
            while (rs.next()) {
                TableData tableData = new TableData();
                String name = rs.getString("name");
                tableData.setName(name);
                tableData.setPropertyName(StringUtils.toCamelCase(name));
                tableData.setType(toEnum(rs.getString("type")));
                tableData.setComment(rs.getString("comment"));
                tableDatas.add(tableData);
            }
            baseData.setTableData(tableDatas);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                    closeConnection(conn);
                } catch (SQLException e) {
                    LOGGER.error("getColumn close ResultSet and connection failure", e);
                }
            }
        }
        return baseData;
    }

    /**
     * 获取数据库连接
     *
     * @return
     */
    public static Connection getConnection() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(Constant.URL, Constant.USER_NAME, Constant.PASS_WORD);
        } catch (Exception e) {
            LOGGER.error("get connection failure", e);
        }
        return conn;
    }

    /**
     * 关闭数据库连接
     *
     * @param conn
     */
    public static void closeConnection(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                LOGGER.error("close connection failure", e);
            }
        }
    }

    /**
     * mysql类型转换
     *
     * @param type 类型
     * @return {@link String}
     */
    private static String toEnum(String type) {
        switch (type) {
            case "varchar":
                type = "String";
                break;
            case "datetime":
                type = "Date";
                break;
            case "int":
                type = "Integer";
                break;
            case "tinyint":
                type = "Boolean";
                break;
            case "decimal":
                type = "BigDecimal";
                break;
            case "text":
                type = "String";
                break;
            default:
                type = "未知类型";
                System.out.print("存在不支持类型！请手写。");
                break;
        }
        return type;
    }

}